package com.example.cadmin.utils;


import com.example.cadmin.entityVo.MyDeptTreeVo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class TreeUtil {
    //转换树结构list
    public static <T  extends MyDeptTreeVo> List<T> buildDeptAll(List<T> treeNodes) {
        List<T> result = new ArrayList<>();
        //list转map
        Map<Integer,T> nodeMap = new LinkedHashMap<>(treeNodes.size());
        for (T map : treeNodes){
            nodeMap.put(map.getId(), map);
        }
        for(T node : nodeMap.values()) {
            T parent = nodeMap.get(node.getParentId());
            if(parent != null && !(node.getId().equals(parent.getId()))){
                parent.getChildren().add(node);
                continue;
            }
            result.add(node);
        }
        return result;
    }
}
